<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- android.qdoc -->
  <title>Porting to Android | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Porting to Android</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Porting to Android</h1>
<span class="subtitle"></span>
<!-- $$$porting-to-android.html-description -->
<div class="descr"> <a name="details"></a>
<p>In this section, we are going to port an existing Qt Application to <a href="android.html">Android</a> and deploy it to the device.</p>
<p>Most Qt applications should be portable to Android with ease, unless they depend on a specific hardware or software feature not supported by Android. If your application is not using any such feature, deployment is probably the only step that demands some changes to your application.</p>
<p>Like most UI applications, Qt applications also depend on resources such as images, icons, translation files, and so on. These resources must be made available on the device as they are required for the application to function correctly.</p>
<p>The most convenient option is to bundle the resources into a qrc file, which gets built into the application binary. This approach reduces the porting effort considerably and provides faster access to the resources. It is also a cross-platform approach, which makes porting to other platforms easier.</p>
<p>By default, all Qt applications can access the contents of a qrc file using the &quot;:/&quot; prefix or the URL scheme prefix, &quot;qrc:&quot;. To know more about qrc files and how they are handled, see <a href="../qtcore/resources.html">the Qt Resource System</a>.</p>
<p>The other approach is to deploy the resources into the package's <code>assets</code> directory. It is the best option if you want to achieve better interoperability with the Android APIs. You can access all resources in the directory using the &quot;assets:&quot; prefix. Unlike qrc, this approach is not a cross-platform solution.</p>
<p>The following step-by-step instructions guide you to port an existing Qt Quick application to Android using the qrc approach:</p>
<ol class="1" type="1"><li>Open the existing project in Qt Creator and configure it with &quot;Android for ARM&quot; kit. For more information, see <a href="http://doc.qt.io/qtcreator/creator-configuring-projects.html">Qt Creator: Configuring Projects</a></li>
<li>Identify all the resources used by your application and add them to one or more qrc files. Qt Creator updates your qmake project file with the &quot;RESOURCES&quot; variable listing the qrc files you added.</li>
<li>To load or refer to the resources in the qrc file from your C++ code, use the &quot;qrc:&quot; scheme followed by the absolute URL. For example, to load the <code>main.qml</code> file from <code>resources.qrc</code>, you can use the following C++ code:<pre class="cpp">

  <span class="type"><a href="../qtquick/qquickview.html">QQuickView</a></span> viewer;
  viewer<span class="operator">.</span>setSource(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span>(<span class="string">&quot;qrc:/qml/main.qml&quot;</span>));
  viewer<span class="operator">.</span>show();

</pre>
<p><b>Note: </b>QML documents can refer to the contents in qrc files using the relative path to the document. Such references do not require the &quot;<code>qrc:</code>&quot; or &quot;<code>:/</code>&quot; prefix.</p></li>
<li>Save the changes to your project and run the application.</li>
</ol>
<p><b>Note: </b>Qt Quick Controls in the application will use native <a href="../qtquickcontrols1/qtquickcontrols-platformnotes.html#android-style">Android style</a> if the target device is running Android 3.0 (API 11) or later. The application should function normally on devices with Android versions earlier than v3.0, but without native style for controls.</p><p>Qt Creator deploys your application on the Android device, if the device is detected by the PC. Otherwise, it tries to run the application on an AVD (Android Virtual Device). You will be prompted to create one if there are no AVDs found.</p>
</div>
<!-- @@@porting-to-android.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
